﻿using Abp.Domain.Repositories;
using Abp.Domain.Services;
using shenghua.Commodity.Commodities;
using shenghua.Order.Orders.Median;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Member.ShoppingCart
{
    public class ShoppingCartManager : DomainService
    {
        private readonly IRepository<ShoppingCartCommodity> _shoppingCartCommodityRepository;
        private readonly CommodityManager _commodityManager;

        public ShoppingCartManager(
            IRepository<ShoppingCartCommodity> shoppingCartCommodityRepository,
            CommodityManager commodityManager
            )
        {
            _shoppingCartCommodityRepository = shoppingCartCommodityRepository;
            _commodityManager = commodityManager;
        }

        /// <summary>
        /// 添加商品到购物车
        /// </summary>
        /// <param name="memberId"></param>
        /// <param name="shoppingCartCommodity"></param>
        /// <returns></returns>
        public async Task<ShoppingCartCommodity> AddCommodityToShoppingCart(Member.Members.Member member, ShoppingCartCommodity shoppingCartCommodity)
        {
            ShoppingCartCommodity old = _shoppingCartCommodityRepository.GetAll().Where(r =>
                r.MemberId == member.Id &&
                r.CommodityId == shoppingCartCommodity.CommodityId &&
                r.ShoppingOptionId == shoppingCartCommodity.ShoppingOptionId
                ).FirstOrDefault();

            if(old == null)
            {
                // 说明还没有添加过
                shoppingCartCommodity.MemberId = member.Id;
                await _shoppingCartCommodityRepository.InsertAsync(shoppingCartCommodity);
                return shoppingCartCommodity;
            }
            else
            {
                // 说明添加过
                old.Count += shoppingCartCommodity.Count;
                await _shoppingCartCommodityRepository.UpdateAsync(old);
                return old;
            }
        }

        /// <summary>
        /// 清空购物车
        /// </summary>
        /// <param name="memberId"></param>
        /// <returns></returns>
        public async Task ClearShoppingCart(long memberId)
        {
            await _shoppingCartCommodityRepository.DeleteAsync(r => r.MemberId == memberId);
        }

        /// <summary>
        /// 得到购物车所有商品
        /// </summary>
        /// <param name="memberId"></param>
        /// <returns></returns>
        public async Task<List<ShoppingCartCommodity>> GetAll(Member.Members.Member member)
        {
            var items = await _shoppingCartCommodityRepository.GetAllListAsync(r => r.MemberId == member.Id);
            return items;
        }

        /// <summary>
        /// 从购物车移除某个商品
        /// </summary>
        /// <param name="memberId"></param>
        /// <param name="commodityId"></param>
        /// <returns></returns>
        public async Task RemoveCommodityFromShoppingCart(long memberId, int shoppingCartCommodityId)
        {
            await _shoppingCartCommodityRepository.DeleteAsync(r => r.MemberId == memberId && r.Id == shoppingCartCommodityId);
        }

        public async Task RemoveCommodityFromShoppingCart(long memberId, int commodityId, int? shoppingOptionId)
        {
            await _shoppingCartCommodityRepository.DeleteAsync(r => r.MemberId == memberId && r.CommodityId == commodityId && r.ShoppingOptionId == shoppingOptionId);
        }
    }
}
